package com.qingfeng.medical.controller.admin.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.qingfeng.medical.base.controller.BaseController;
import com.qingfeng.medical.base.log.annotation.Log;
import com.qingfeng.medical.base.log.enums.BusinessType;
import com.qingfeng.medical.base.result.Result;
import com.qingfeng.medical.base.ro.EnumsRo;
import com.qingfeng.medical.biz.hosp.domain.vo.ScheduleDepPatientVo;
import com.qingfeng.medical.biz.hosp.service.ScheduleService;
import com.qingfeng.medical.biz.order.domain.entity.OrderInfo;
import com.qingfeng.medical.biz.order.domain.enums.OrderStatusEnum;
import com.qingfeng.medical.biz.order.domain.vo.OrderInfoVo;
import com.qingfeng.medical.biz.order.service.OrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 订单表
 *
 * @author 清风学Java
 * @date 2023-04-28 17:10:43
 */
@Api(tags = "订单表管理")
@RestController
@RequestMapping("/admin/info/orderInfo")
@CrossOrigin
@Validated
public class OrderInfoController extends BaseController {

    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation(value = "订单表分页列表", notes = "订单表分页列表")
    @GetMapping("/list")
    @Log(title = "预约订单管理", businessType = BusinessType.SELECT, message = "订单表分页列表")
    public Result<IPage<OrderInfo>> orderInfoList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @RequestParam("page") Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @RequestParam("limit") Long limit,
            @ApiParam(name = "depName", value = "科室名", required = false)
            @RequestParam("depName") String depName,
            @ApiParam(name = "patientName", value = "门诊", required = false)
            @RequestParam("patientName") String patientName,
            @ApiParam(name = "orderStatus", value = "订单状态", required = false)
            @RequestParam("orderStatus") OrderStatusEnum orderStatus,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
            @ApiParam(name = "orderDate", value = "订单日期", required = false)
            @RequestParam("orderDate") LocalDate orderDate) {

        // TODO 完善订单信息，是否取号
        return Result.ok(
                orderInfoService.orderList(new Page<OrderInfo>(page, limit),
                        orderStatus,
                        depName,
                        patientName,
                        orderDate)
        );
    }

    @ApiOperation(value = "返回订单状态列表", notes = "返回订单状态列表")
    @GetMapping("/status")
    @Log(title = "预约订单管理", businessType = BusinessType.SELECT, message = "返回订单状态列表")
    public Result<List<EnumsRo>> orderStatusList() {
        return Result.ok(
                Arrays.stream(OrderStatusEnum.values())
                        .map(o -> EnumsRo.builder()
                                .label(o.getMsg())
                                .value(o.name())
                                .build()
                        )
                        .collect(Collectors.toList())
        );
    }

    @ApiOperation(value = "查询医师下的科室门诊关系", notes = "查询医师下的科室门诊关系")
    @GetMapping("/dep/patient/doctorId")
    @Log(title = "预约订单管理", businessType = BusinessType.SELECT, message = "查询医师下的科室门诊关系")
    public Result<List<ScheduleDepPatientVo>> depPatientByDoctorId() {
        List<ScheduleDepPatientVo> scheduleDepPatientList = scheduleService.findDepPatientByDoctorId(getUserId());
        return Result.ok(scheduleDepPatientList);
    }


    @ApiOperation(value = "查询当前医生下预约成功的用户订单", notes = "查询当前医生下预约成功的用户订单")
    @GetMapping("/doctor/orderInfo")
    @Log(title = "预约订单管理", businessType = BusinessType.SELECT, message = "查询当前医生下预约成功的用户订单")
    public Result<OrderInfoVo> doctorOrderInfoList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @RequestParam("page") Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @RequestParam("limit") Long limit,
            @ApiParam(name = "depId", value = "科室门诊Id", required = true)
            @RequestParam("depId") Long depId,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
            @ApiParam(name = "orderDate", value = "订单日期", required = true)
            @RequestParam("orderDate") LocalDate orderDate) {
        // 查询出所有
        return Result.ok(orderInfoService.findDoctorOrderInfoList(
                new Page<OrderInfo>(page, limit),
                depId,
                orderDate));
    }

    @ApiOperation(value = "根据Id查询订单信息", notes = "根据Id查询订单信息")
    @GetMapping("/info/{id}")
    @Log(title = "预约订单管理", businessType = BusinessType.SELECT, message = "根据Id查询订单信息")
    public Result<OrderInfo> orderInfo(@PathVariable("id") Long id) {
        return Result.ok(orderInfoService.getById(id));
    }
}
